package com.java.base.exercise.designmode.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author RenPu
 * @Date 2021/6/27 14:07
 * @Version 1.0
 * @Description: 设计模式--观察者模式3.0   使用接口的多态封装多个观察者对应一个被观察者
 **/
public class ObersverV3 {

    public static void main(String[] args) {

        Childv3 child = new Childv3();
        child.weakUp();
    }


}


/**
 * 被观察者
 */
class Childv3 {
    boolean cry = false;
    List<Observerter> list = new ArrayList<>();

    {
        list.add(new Dad3());
        list.add(new Momv1());
        list.add(new Dogv1());
    }


    public boolean isCry() {
        return cry;
    }

    public void weakUp() {
        cry = true;

        WeakUpEvent weakUpEvent = new WeakUpEvent(System.currentTimeMillis(), "bed", this);
        for (Observerter observerter : list) {
            observerter.actionOnwearkUp(weakUpEvent);
        }
    }


}


/**
 * 事件类
 */
class WeakUpEvent {
    long timestamp;
    String loc;
    Childv3 childv3;

    public WeakUpEvent(long timestamp, String loc, Childv3 childv3) {
        this.timestamp = timestamp;
        this.loc = loc;
        this.childv3 = childv3;
    }

    public WeakUpEvent() {
    }
}


interface Observerter {
    void actionOnwearkUp(WeakUpEvent weakUpEvent);
}


/**
 * 观察者
 */
class Dad3 implements Observerter {

    //爸爸喂
    public void feed() {
        System.out.println("dad  feeding ...........");
    }

    @Override
    public void actionOnwearkUp(WeakUpEvent weakUpEvent) {
        feed();
    }
}


/**
 * 观察者
 */
class Momv1 implements Observerter {

    //妈妈抱
    public void embrace() {
        System.out.println("Mum  embracing ...........");
    }

    @Override
    public void actionOnwearkUp(WeakUpEvent weakUpEvent) {
        embrace();
    }
}


/**
 * 观察者
 */
class Dogv1 implements Observerter {
    public void call() {
        System.out.println("dog call ...........");
    }

    @Override
    public void actionOnwearkUp(WeakUpEvent weakUpEvent) {
        call();
    }
}
